package com.umeng.analytics.pro;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Build;
import android.text.TextUtils;

import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.a;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy.Type;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.KeyStore;

import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

public class aw {
    private String a;
    private String b;
    private int c;
    private Context d;
    private av e;

    public aw(Context arg2) {
        this.b = "10.0.0.172";
        this.c = 80;
        this.d = arg2;
        this.a = this.a(arg2);
    }

    private String a(Context arg4) {
        StringBuffer v0 = new StringBuffer();
        v0.append("Android");
        v0.append("/");
        v0.append("6.1.1");
        v0.append(" ");
        try {
            StringBuffer v1_1 = new StringBuffer();
            v1_1.append(bv.v(arg4));
            v1_1.append("/");
            v1_1.append(bv.b(arg4));
            v1_1.append(" ");
            v1_1.append(Build.MODEL);
            v1_1.append("/");
            v1_1.append(Build.VERSION.RELEASE);
            v1_1.append(" ");
            v1_1.append(bw.a(AnalyticsConfig.getAppkey(arg4)));
            v0.append(URLEncoder.encode(v1_1.toString(), "UTF-8"));
        } catch (Throwable v1) {
        }

        return v0.toString();
    }

    private byte[] a(byte[] arg9, String arg10) {
        byte[] v0_4;
        InputStream v3 = null;
        int v0_3;
        URLConnection v2 = null;
        byte[] v1 = null;
        try {
            if (this.e != null) {
                this.e.a();
            }
            v2 = this.c() ? new URL(arg10).openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.b, this.c))) : new URL(arg10).openConnection();
            ((HttpURLConnection) v2).setRequestProperty("X-Umeng-UTC", String.valueOf(System.currentTimeMillis()));
            ((HttpURLConnection) v2).setRequestProperty("X-Umeng-Sdk", this.a);
            ((HttpURLConnection) v2).setRequestProperty("Msg-Type", "envelope/json");
            ((HttpURLConnection) v2).setRequestProperty("Content-Type", "envelope/json");
            ((HttpURLConnection) v2).setConnectTimeout(10000);
            ((HttpURLConnection) v2).setReadTimeout(30000);
            ((HttpURLConnection) v2).setRequestMethod("POST");
            ((HttpURLConnection) v2).setDoOutput(true);
            ((HttpURLConnection) v2).setDoInput(true);
            ((HttpURLConnection) v2).setUseCaches(false);
            if (Build.VERSION.SDK_INT < 8) {
                System.setProperty("http.keepAlive", "false");
            }

            OutputStream v0_1 = ((HttpURLConnection) v2).getOutputStream();
            v0_1.write(arg9);
            v0_1.flush();
            v0_1.close();
            if (this.e != null) {
                this.e.b();
            }

            int v5 = ((HttpURLConnection) v2).getResponseCode();
            String v0_2 = ((HttpURLConnection) v2).getHeaderField("Content-Type");
            if (TextUtils.isEmpty(((CharSequence) v0_2))) {
                v0_3 = 0;
            } else if (v0_2.equalsIgnoreCase("application/thrift")) {
                v0_3 = 1;
            } else {
                v0_3 = 0;
            }
            if (v5 == 200 && v0_3 != 0) {
                by.c("Send message to " + arg10);
                v3 = v2.getInputStream();
                v0_4 = bw.b(v3);
                return v0_4;
            }
            return null;
        } catch (Throwable v0) {
            by.e("IOException,Failed to send message.", v0);
            return null;
        } finally {
            if (v2 != null) {
                ((HttpURLConnection) v2).disconnect();
            }

            bw.c(v3);
        }
    }

    protected void a() {
        InputStream v1_3;
        InputStream v0 = null;
        try {
            KeyStore v1_1 = KeyStore.getInstance(KeyStore.getDefaultType());
            v1_1.load(null, null);
            bb v2 = new bb(v1_1);
            ((SSLSocketFactory) v2).setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            HttpGet v1_2 = new HttpGet("https://uop.umeng.com");
            BasicHttpParams v3 = new BasicHttpParams();
            HttpProtocolParams.setVersion(((HttpParams) v3), HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(((HttpParams) v3), "ISO-8859-1");
            HttpProtocolParams.setUseExpectContinue(((HttpParams) v3), true);
            ConnManagerParams.setTimeout(((HttpParams) v3), 10000);
            HttpConnectionParams.setConnectionTimeout(((HttpParams) v3), 10000);
            HttpConnectionParams.setSoTimeout(((HttpParams) v3), 10000);
            SchemeRegistry v4 = new SchemeRegistry();
            v4.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            v4.register(new Scheme("https", ((SocketFactory) v2), 443));
            v0 = new DefaultHttpClient(new ThreadSafeClientConnManager(v3, v4), v3).execute(v1_2).getEntity().getContent();
            if (v0 != null) {
                ByteArrayOutputStream v1_4 = new ByteArrayOutputStream();
                byte[] v2_1 = new byte[1024];
                while (true) {
                    int v3_1 = v0.read(v2_1);
                    if (v3_1 == -1) {
                        break;
                    }

                    v1_4.write(v2_1, 0, v3_1);
                }
                v1_4.close();
                String v2_2 = new String(v1_4.toByteArray(), "UTF-8");
                if (!TextUtils.isEmpty(((CharSequence) v2_2)) && v2_2.length() > 0 && v2_2.length() < 50) {
                    SharedPreferences v1_5 = ba.a(this.d);
                    if (v1_5 != null) {
                        v1_5.edit().putString("uopdta", v2_2).commit();
                    }
                }
            }
        } catch (Throwable v1) {
        } finally {
            if (v0 != null) {
                try {
                    v0.close();
                } catch (Throwable v0_1) {
                }
            }
        }
    }

    public void a(av arg1) {
        this.e = arg1;
    }

    public byte[] a(byte[] arg4) {
        byte[] v1 = null;
        this.b();
        int v0 = 0;
        while (v0 < com.umeng.analytics.a.h.length) {
            v1 = this.a(arg4, com.umeng.analytics.a.h[v0]);
            if (v1 == null) {
                if (this.e != null) {
                    this.e.d();
                }

                ++v0;
                continue;
            } else if (this.e != null) {
                this.e.c();
            }

            return v1;
        }

        return v1;
    }

    private void b() {
        int v5 = 2;
        String v0 = af.a(this.d).b().d("");
        String v1 = af.a(this.d).b().c("");
        if (!TextUtils.isEmpty(((CharSequence) v0))) {
            com.umeng.analytics.a.f = bt.b(v0);
        }

        if (!TextUtils.isEmpty(((CharSequence) v1))) {
            com.umeng.analytics.a.g = bt.b(v1);
        }

        String[] v0_1 = new String[v5];
        v0_1[0] = com.umeng.analytics.a.f;
        v0_1[1] = com.umeng.analytics.a.g;
        com.umeng.analytics.a.h = v0_1;
        int v0_2 = bg.a(this.d).b();
        if (v0_2 != -1) {
            if (v0_2 == 0) {
                v0_1 = new String[v5];
                v0_1[0] = com.umeng.analytics.a.f;
                v0_1[1] = com.umeng.analytics.a.g;
                com.umeng.analytics.a.h = v0_1;
            } else if (v0_2 == 1) {
                v0_1 = new String[v5];
                v0_1[0] = com.umeng.analytics.a.g;
                v0_1[1] = com.umeng.analytics.a.f;
                com.umeng.analytics.a.h = v0_1;
            }
        }
    }

    private boolean c() {
        if (this.d.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", this.d.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
            boolean v0 = false;
            return v0;
        }

        try {
            Object v0_2 = this.d.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (!bv.a(this.d, "android.permission.ACCESS_NETWORK_STATE")) {
                return false;
            }

            NetworkInfo v0_3 = ((ConnectivityManager) v0_2).getActiveNetworkInfo();
            if (v0_3 == null) {
                return false;
            }

            if (v0_3.getType() == 1) {
                return false;
            }

            String v0_4 = v0_3.getExtraInfo();
            if (v0_4 == null) {
                return false;
            }

            if (!v0_4.equals("cmwap") && !v0_4.equals("3gwap") && !v0_4.equals("uniwap")) {
                return false;
            }
        } catch (Throwable v0_1) {
            return false;
        }

        return true;
    }
}

